package com.zhanggm.student.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhanggm.student.config.ResponseResult;
import com.zhanggm.student.entity.Classes;
import com.zhanggm.student.service.IClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/classes")
public class ClassesController {

    @Autowired
    private IClassesService iClassesService;


    @GetMapping("getAllClasses")
    public ResponseResult getAllClasses(){
        return ResponseResult.success(iClassesService.list());
    }

    @PostMapping("save")
    public ResponseResult save(@RequestBody Classes classes){
        List<Classes> classesList = classes.getClassesList();
        //classesList没有的Id，从表中删除
        List<Integer> classesIdList = classesList.stream().map(c -> c.getId()).collect(Collectors.toList());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.notIn("id",classesIdList);
        iClassesService.remove(queryWrapper);
        //保存数据
        if(classesList!=null){
            classesList.forEach(c->{
                iClassesService.saveOrUpdate(c);
            });
        }
        return ResponseResult.success(iClassesService.list());
    }

}
